package bsc.war.ajax;

import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.ScoreCard;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class AddScoreCardRelation extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String modelView = "addScoreCardRelation";
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            // send the id or the scorecard root
            Long idScoreCard = request.getParameter("scoreCard") != null && NumberUtils.isNumber(request.getParameter("scoreCard")) ? Long.parseLong(request.getParameter("scoreCard")) : null;

            if (idScoreCard == null) {
                modelView = "chooseScoreCardRoot";

                //List of all Scorecards
                List<ScoreCard> parentScoreCardList = ScoreCardUtils.getAllScoreCards(request, delegate);

                //List of all DimensionalCuts
                List<DimensionalCut> dimensionalCuts = ScoreCardUtils.getDimensionalCuts(request, delegate);

                //Se obtiene los strings de los cuts
                Map<DimensionalCut, String> dimensionalCutsMembers = ScoreCardUtils.getDimensionalCutsMembersStrings(request, delegate, dimensionalCuts);

                request.setAttribute("parentScoreCardList", parentScoreCardList);
                request.setAttribute("dimensionalCutsMembers", dimensionalCutsMembers);
                
            } else {
                //List of all DimensionalCuts
                List<DimensionalCut> dimensionalCuts = ScoreCardUtils.getDimensionalCuts(request, delegate);

                //Se obtiene los strings de los cuts
                Map<DimensionalCut, String> dimensionalCutsMembers = ScoreCardUtils.getDimensionalCutsMembersStrings(request, delegate, dimensionalCuts);


                //Scorecard Root
                ScoreCard scoreCardRoot = delegate.findScoreCard(idScoreCard);
                //All the scorecards
                List<ScoreCard> parentScoreCardListTotal = ScoreCardUtils.getAllScoreCards(request, delegate);

                List<ScoreCard> parentScoreCardList = new ArrayList<ScoreCard>();
                //List with the parent scoercard
                parentScoreCardList.add(scoreCardRoot);


                //Tree of scorecards of the root
                List<ScoreCard> treeMembers = ScoreCardUtils.getScoreCards(delegate, scoreCardRoot);
                //List of posible scorecards.
                List<ScoreCard> childScoreCardList = new ArrayList<ScoreCard>();

                for (ScoreCard scoreCard : parentScoreCardListTotal) {
                    List<ScoreCard> upList = ScoreCardUtils.getScoreCards(delegate, scoreCard);
                    if (!treeMembers.contains(scoreCard) && !upList.contains(scoreCardRoot)) {
                        childScoreCardList.add(scoreCard);
                    }
                }

                //Se elimina el primer miembro de la lista de treeMembers porque es el root y no existen relaciones entre si mismo.
                treeMembers.remove(scoreCardRoot);


                //Look for the direct sons scorecards
                List<ScoreCard> directScoreCards = delegate.findOtherScoreCards(idScoreCard);

                HashMap<ScoreCard, Double> relationFactors = new HashMap<ScoreCard, Double>();
                for (ScoreCard child : directScoreCards) {
                    relationFactors.put(child, delegate.findScoreCardRelation(idScoreCard, child.getIdScoreCard()).getPercentFactor());
                }


                /*
                HashMap<ScoreCard, List<ScoreCard>> otherScores = new HashMap<ScoreCard, List<ScoreCard>>();
                HashMap<ScoreCard, HashMap<ScoreCard, Double>> factores = new HashMap<ScoreCard, HashMap<ScoreCard, Double>>();
                for(ScoreCard score : parentScoreCardList)
                {
                HashMap<ScoreCard, Double> relationFactors = new HashMap<ScoreCard, Double>();
                List<ScoreCard> others = delegate.findOtherScoreCards(score.getIdScoreCard());
                otherScores.put(score, others);
                for(ScoreCard other : others)
                {
                ScoreCardRelation relation = delegate.findScoreCardRelation(score.getIdScoreCard(), other.getIdScoreCard());
                relationFactors.put(other, relation.getPercentFactor());
                }
                factores.put(score, relationFactors);
                }*/

                Collections.sort(childScoreCardList);

                request.setAttribute("idScoreCard", idScoreCard);
                request.setAttribute("parentScoreCardList", parentScoreCardList);
                request.setAttribute("childScoreCardList", childScoreCardList);
                request.setAttribute("relationFactors", relationFactors);
                request.setAttribute("directScoreCards", directScoreCards);
                request.setAttribute("dimensionalCutsMembers", dimensionalCutsMembers);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView(modelView);
    }
}
